var boxy = null;

//提示框
function alertMessage(showMessage, showType, showTime)
{
    showMessage = (undefined == showMessage ? "" : showMessage);
    if(showMessage)
    {
        showTime = 10000;
        showTime = (undefined == showTime ? 1 : showTime);
        showType = (undefined == showType ? 0 : showType);
        var showImg = showType == 1 ? "success" : "warning";
        swal({   title: "",   text: showMessage, type: showImg,   timer: showTime*1000 });
    }
}

//指定位置，展示提示信息
function showMessage(message, id)
{
    $('#message_'+id).html(message);
    $('#message_'+id).show();
}
//指定位置， 隐藏提示信息
function hideMessage(id)
{
    $('#message_'+id).html('');
}

//邮箱格式验证
function checkEmail(email)
{
    var result = false;
    //对电子邮件的验证
    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if(myreg.test(email))
    {
        result = true;
    }
    return result;
}

//手机号码格式验证
function checkMobile(mobile)
{
    var result = false;
    //对电子邮件的验证
    var myreg = /^\d{11}$/;
    if(myreg.test(mobile))
    {
        result = true;
    }
    return result;
}

//延时跳转到某个地址
function delayGoToPage(url,time)
{
    setTimeout('goToPage("'+url+'")',time);
}

//跳转到某个地址
function goToPage(url)
{
    location.href = url;
}

//关闭弹窗
function closeBoxy()
{
    boxy.hideAndUnload();
}

//ajax提交表单
function ajaxPost()
{
    var target,query,form;
    form = $('#data_form');
    var target = form.attr('action');
    query = form.serialize();
    $('#submit_btn').addClass('disabled').prop('disabled',true);
    $.post(target,query).success(function(data){
        //console.log(data);
        if (data.status==1) {
            alertMessage(data.info + ' 页面即将自动跳转~', 1, 2);
            if (data.url) {
                delayGoToPage(data.url, 2000);
            }
        }else{
            alertMessage(data.info, '', 2);
        }
        $('#submit_btn').removeClass('disabled').prop('disabled',false);
    });
    return false;
}

//刷新验证码
function changeVerify()
{
    $('#verify_img').prop("src", "/Common-imageVerify-"+new Date().getTime());
}

//保存用户组信息
function saveUserGroup()
{
    var group_name = $.trim($('#group_name').val());
    var check_group_name = false;
    if (!group_name) {
        showMessage('请输入1-12位中文、英文、数字、下划线', 'group_name');
        hideMessage('group_name_info');
    } else {
        var eg = /^[A-Za-z0-9_\u4E00-\u9FA5]+$/;
        var check_data = eg.test(group_name);
        if (!check_data) {
            showMessage('请输入1-12位中文、英文、数字、下划线', 'group_name');
            hideMessage('group_name_info');
        } else {
            check_group_name = true;
            hideMessage('group_name');
        }
    }
    
    var description = $.trim($('#description').val());
    var check_description = false;
    if (!description) {
        showMessage('请输入1-50个字符', 'description');
        hideMessage('description_info');
    } else if(1 > description.length || 50 < description.length) {
        showMessage('请输入1-50个字符', 'description');
        hideMessage('description_info');
    } else {
        check_description = true;
        hideMessage('description');
    }
    
    if(check_group_name
       && check_description)
    {
        ajaxPost();
    }
}

//删除用户组
function deleteGroup(group_id)
{
    swal({
        title: "是否确定删除该用户组？",
        text: "该步操作无法撤回！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#0C7AB9",
        cancelButtonText: "取消",
        confirmButtonText: "确定删除",
        closeOnConfirm: false
        },
        function(){
            $.ajax({
                type:'get',
                dataType:'json',
                url: delete_group_url+'/group_id/'+group_id,
                data:{
                    option:'ajax'
                },
                success:function(data){
                    if (data.status==1) {
                        alertMessage(data.info, 1, 2);
                        if (data.url) {
                            delayGoToPage(data.url, 3000);
                        }
                    }else{
                        alertMessage(data.info, '', 2);
                    }
                }
            });
        }
    );
}

//保存用户信息
function saveUser()
{
    var user_name = $.trim($('#user_name').val());
    var check_user_name = false;
    if (!user_name) {
        showMessage('请输入3-18位字母、数字或下划线', 'user_name');
        hideMessage('user_name_info');
    } else if(3 > user_name.length || 18 < user_name.length) {
        showMessage('请输入3-18位字母、数字或下划线', 'user_name');
        hideMessage('user_name_info');
    } else {
        var eg = /^[A-Za-z0-9_]+$/;
        var check_data = eg.test(user_name);
        if (!check_data) {
            showMessage('请输入3-18位字母、数字或下划线', 'user_name');
            hideMessage('user_name_info');
        } else {
            check_user_name = true;
            hideMessage('user_name');
        }
    }
    
    //如果是新增用户，就需要判断是否输入密码
    var user_id = $('#user_id').val();
    var password = $.trim($('#password').val());
    var check_password = false;
    if (!user_id) {
        if (!password) {
            showMessage('请输入6-16位字母、数字', 'password');
            hideMessage('password_info');
        } else if(6 > password.length || 16 < password.length){
            showMessage('请输入6-16位字母、数字', 'password');
            hideMessage('password_info');
        } else {
            var eg = /^[A-Za-z0-9]+$/;
            var check_data = eg.test(password);
            if (!check_data) {
                showMessage('请输入6-16位字母、数字', 'password');
                hideMessage('password_info');
            } else {
                check_password = true;
                hideMessage('password');
            }
        }
    } else {
        if (!password) {
            check_password = true;
        } else if(6 > password.length || 16 < password.length){
            showMessage('请输入6-16位字母、数字', 'password');
            hideMessage('password_info');
        } else {
            var eg = /^[A-Za-z0-9]+$/;
            var check_data = eg.test(password);
            if (!check_data) {
                showMessage('请输入6-16位字母、数字', 'password');
                hideMessage('password_info');
            } else {
                check_password = true;
                hideMessage('password');
            }
        }
    }
    
    var nick_name = $.trim($('#nick_name').val());
    var check_nick_name = false;
    if (!nick_name) {
        showMessage('请输入1-10个字母、数字、中文或下划线', 'nick_name');
        hideMessage('nick_name_info');
    } else if(1 > nick_name.length || 10 < nick_name.length){
        showMessage('请输入1-10个字母、数字、中文或下划线', 'nick_name');
        hideMessage('nick_name_info');
    } else {
        var eg = /^[A-Za-z0-9_\u4E00-\u9FA5]+$/;
        var check_data = eg.test(nick_name);
        if (!check_data) {
            showMessage('请输入1-10个字母、数字、中文或下划线', 'nick_name');
            hideMessage('nick_name_info');
        } else {
            check_nick_name = true;
            hideMessage('nick_name');
        }
    }
    
    var mobile = $.trim($('#mobile').val());
    var check_mobile = false;
    if (!mobile) {
        showMessage('请输入可用的手机号码', 'mobile');
        hideMessage('mobile_info');
    } else if (!checkMobile(mobile)) {
        showMessage('请输入可用的手机号码', 'mobile');
        hideMessage('mobile_info');
    } else {
        check_mobile = true;
        hideMessage('mobile');
    }
    
    if(check_user_name
       && check_nick_name
       && check_password
       && check_mobile)
    {
        ajaxPost();
    }
}

//删除用户
function updateUser(url, message)
{
    swal({
        title: message,
        text: "",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#0C7AB9",
        cancelButtonText: "取消",
        confirmButtonText: "确认",
        closeOnConfirm: false
        },
        function(){
            $.ajax({
                type:'get',
                dataType:'json',
                url: url,
                data:{
                    option:'ajax'
                },
                success:function(data){
                    if (data.status==1) {
                        alertMessage(data.info, 1, 2);
                        if (data.url) {
                            delayGoToPage(data.url, 3000);
                        }
                    }else{
                        alertMessage(data.info, '', 2);
                    }
                }
            });
        }
    );
}

//保存菜单
function saveMenu()
{
    var name = $.trim($('#name').val());
    var check_name = false;
    if (!name) {
        showMessage('请输入菜单名', 'name');
    } else {
        check_name = true;
        hideMessage('name');
    }
    
    if(check_name)
    {
        document.data_form.submit();
    }
}

//删除菜单
function deleteMenu(url)
{
    swal({
        title: "是否确定删除该菜单？",
        text: "该步操作无法撤回！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#0C7AB9",
        cancelButtonText: "取消",
        confirmButtonText: "确定删除",
        closeOnConfirm: false
        },
        function(){
            window.location.href = url;
        }
    );
}

//保存计费方式
function saveRechargeMethod()
{
    var recharge_method_name = $.trim($('#recharge_method_name').val());
    var check_recharge_method_name = false;
    if (!recharge_method_name) {
        showMessage('请输入收费方式', 'recharge_method_name');
    } else {
        recharge_method_name = true;
        hideMessage('recharge_method_name');
    }
    
    var cost_type = $('input:radio[name=cost_type]:checked').val();
    check_time_count = false;
    if (2 == cost_type) {
        var time_count = $.trim($('#time_count').val());
        var check_time_count = false;
        if (!time_count) {
            showMessage('请输入时间数量', 'cost_type');
        } else {
            var eg = /^[1-9]*[1-9][0-9]*$/;
            var check_data = eg.test(time_count);
            if (!check_data) {
                showMessage('时间数量请输入正整数', 'cost_type');
            } else {
                check_time_count = true;
                hideMessage('cost_type');
            }
        }
    } else {
        check_time_count = true;
        hideMessage('cost_type');
    }
    
    var price = $.trim($('#price').val());
    var check_price = false;
    if (!price) {
        showMessage('请输入价格', 'price');
    } else {
        var eg = /^(0|[1-9]\d*)$/;
        var check_data = eg.test(price);
        if (!check_data) {
            showMessage('价格请输入非负整数', 'price');
        } else {
            check_price = true;
            hideMessage('price');
        }
    }
    
    if(recharge_method_name
       && check_time_count
       && check_price)
    {
        ajaxPost();
    }
}

//停用收费方式
function updateRechargeMethod(url, message)
{
    swal({
        title: message,
        text: "",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#0C7AB9",
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        closeOnConfirm: false
        },
        function(){
            $.ajax({
                type:'get',
                dataType:'json',
                url: url,
                data:{
                    option:'ajax'
                },
                success:function(data){
                    if (data.status==1) {
                        alertMessage(data.info + ' 页面即将自动跳转~', 1, 2);
                        if (data.url) {
                            delayGoToPage(data.url, 2000);
                        }
                    }else{
                        alertMessage(data.info, '', 2);
                    }
                }
            });
        }
    );
}

//保存影院用户
function saveCinemaUser()
{
    //是否新增
    var check_host_name = true;
    if (1 == is_add) {
        check_host_name = false;
        var host_name = $.trim($('#host_name').val());
        if (!host_name) {
            showMessage('请输入主机名(英文、数字或中划线，最长18位)', 'host_name');
        } else {
            var eg = /^[A-Za-z0-9-]+$/;
            var check_data = eg.test(host_name);
            if (!check_data) {
                showMessage('请输入主机名(英文、数字或中划线，最长18位)', 'host_name');
            } else {
                check_host_name = true;
                hideMessage('host_name');
            }
        }
    }
    
    //影院地址，地区：
    check_area = false;
    var mutil_level_data_1_id_1 = $('#mutil_level_data_1_id_1').val();
    var mutil_level_data_1_id_2 = $('#mutil_level_data_1_id_2').val();
    var mutil_level_data_1_id_3 = $('#mutil_level_data_1_id_3').val();
    if (!mutil_level_data_1_id_1) {
        check_area = false;
        showMessage('请选择影院地区', 'area');
    } else {
        check_area = true;
        hideMessage('area');
    }
    if ('none' != $('#mutil_level_data_1_id_2').css('display')) {
        if (!mutil_level_data_1_id_2) {
            check_area = false;
            showMessage('请选择影院地区', 'area');
        } else {
            check_area = true;
            hideMessage('area');
        }
    }
    if ('none' != $('#mutil_level_data_1_id_3').css('display')) {
        if (!mutil_level_data_1_id_3) {
            check_area = false;
            showMessage('请选择影院地区', 'area');
        } else {
            check_area = true;
            hideMessage('area');
        }
    }
    
    var address = $.trim($('#address').val());
    var check_address = false;
    if (!address) {
        showMessage('请输入地址（最长40个字）', 'address');
    } else {
        check_address = true;
        hideMessage('address');
    }
    
    var contacter = $.trim($('#contacter').val());
    var check_contacter = false;
    if (!contacter) {
        showMessage('请输入中文或英文，最长15个字', 'contacter');
    } else if(1 > contacter.length || 15 < contacter.length){
        showMessage('请输入中文或英文，最长15个字', 'contacter');
    } else {
        var eg = /^[A-Za-z\u4E00-\u9FA5]+$/;
        var check_data = eg.test(contacter);
        if (!check_data) {
            showMessage('请输入中文或英文，最长15个字', 'contacter');
        } else {
            check_contacter = true;
            hideMessage('contacter');
        }
    }
    
    var phone = $.trim($('#phone').val());
    var check_phone = false;
    if (!phone) {
        showMessage('请输入11位的手机号', 'phone');
    } else if (!checkMobile(phone)) {
        showMessage('请输入11位的手机号', 'phone');
        hideMessage('mobile_info');
    } else {
        check_phone = true;
        hideMessage('phone');
    }
    
    //代理商，不选就是平台用户
    var agent_id = $.trim($('#agent_id').val());
    var check_agent_id = true;
    //判断代理商状态，如果是停用的，就不能选
    var agent_name = $("select[name=agent_id]").find("option:selected").text();
    if (-1 != agent_name.indexOf('(停用)')) {
        check_agent_id = false;
        showMessage('不能选择停用的代理商', 'agent_id');
    } else {
        hideMessage('agent_id');
    }
    
    //影院类型
    var cinema_type = $.trim($('#cinema_type').val());
    
    //是否试用
    var is_try = $.trim($('#is_try').val());
    var try_day_max = $.trim($('#try_day_max').val());
    var check_try_day_max = false;
    
    //影院类型
    var type = $('#type').val();
    
    //试用，需要填写试用天数
    if (1 == is_try) {
        $('#try_day_max').show();
        if (!try_day_max) {
            showMessage('请填写正确的试用天数', 'try_day_max');
        } else {
            hideMessage('try_day_max');
            check_try_day_max = true;
        }
    } else {
        try_day_max = 0;
        check_try_day_max = true;
    }
    
    //计费方式
    var recharge_method_id = $('#recharge_method_id').val();
    
    //小影院2016计费方式
    var billing_video_play_type = $('#billing_video_play_type').val();
    
    //试用，Kmi影院类型，小帅影院类型，不用完整填写计费信息
    if (1 == is_try
        || 3 == type
        || 4 == type
        || 0 < recharge_method_id
        || 0 < billing_video_play_type
        || 0 < agent_billing_video_play_type) {
        check_hall_price = true;
        check_buy_hall_count = true;
        check_period_count = true;
        check_deduct_period = true;
    } else {
        var hall_price = $.trim($('#hall_price').val());
        var check_hall_price = false;
        if (!hall_price) {
            showMessage('请填写计费金额', 'hall_price');
        } else if(hall_price) {
            //var eg = /^[1-9]*[1-9][0-9]*$/;
            var eg = /^\d+(\.\d{1,2})?$/;
            var check_data = eg.test(hall_price);
            if (!check_data) {
                showMessage('请填写计费金额', 'hall_price');
            } else {
                check_hall_price = true;
                hideMessage('hall_price');
            }
        }
        
        var buy_hall_count = $.trim($('#buy_hall_count').val());
        var check_buy_hall_count = false;
        if (!buy_hall_count) {
            showMessage('请输入授权房间数量，最大为数字99', 'buy_hall_count');
        } else if(buy_hall_count) {
            var eg = /^[1-9]*[1-9][0-9]*$/;
            var check_data = eg.test(buy_hall_count);
            if (!check_data) {
                showMessage('请输入授权房间数量，最大为数字99', 'buy_hall_count');
            } else {
                check_buy_hall_count = true;
                hideMessage('buy_hall_count');
            }
        }
        
        var period_count = $.trim($('#period_count').val());
        var check_period_count = false;
        if (!period_count) {
            showMessage('请输入合同周期的月数，最大为数字99', 'period_count');
        } else if(period_count) {
            var eg = /^[1-9]*[1-9][0-9]*$/;
            var check_data = eg.test(period_count);
            if (!check_data) {
                showMessage('请输入授权房间数量，最大为数字99', 'period_count');
            } else {
                check_period_count = true;
                hideMessage('period_count');
            }
        }
        
        var deduct_period = $.trim($('#deduct_period').val());
        var check_deduct_period = false;
        if (!deduct_period) {
            showMessage('请选择扣费周期', 'deduct_period');
        } else {
            check_deduct_period = true;
            hideMessage('deduct_period');
        }
    }
    
    if(check_host_name 
       && check_area
       && check_address
       && check_contacter
       && check_phone
       && check_agent_id
       && check_hall_price
       && check_buy_hall_count
       && check_period_count
       && check_deduct_period
       && check_try_day_max)
    {
        ajaxPost();
    }
}

//是否立即数据计费方式：试用，Kmi类型，小帅类型，不用立即输入
function isMustInputMoneyInfo()
{
    var recharge_method_id = $('#recharge_method_id').val();
    var is_try = $('#is_try').val();
    var type = $('#type').val();
    
    if (1 == is_try) {
        $('.not_try_data_box').hide();
        $('.try_data_box').show();
    } else {
        $('.try_data_box').hide();
        $('.not_try_data_box').show();
        
        if (0 < recharge_method_id) {
            $('.not_try_data_box').hide();
        }
    }
    
    if (3 == type
        || 4 == type) {
        $('.try_data_box').hide();
        $('.not_try_data_box').hide();
    }
}

//计算每期扣费总计
function calculatePeriodMoney()
{
    var hall_price = $.trim($('#hall_price').val());
    var buy_hall_count = $.trim($('#buy_hall_count').val());
    var deduct_period = $.trim($('#deduct_period').val());
    $('#period_money').html('');
    if (0 < hall_price
        && 0 < buy_hall_count
        && 0 < deduct_period) {
        //var eg = /^[1-9]*[1-9][0-9]*$/;
        var eg = /^\d+(\.\d{1,2})?$/;
        check_hall_price = eg.test(hall_price);
        check_buy_hall_count = eg.test(buy_hall_count);
        check_deduct_period = eg.test(deduct_period);
        if (check_hall_price
            && check_buy_hall_count
            && check_deduct_period) {
            var period_money = hall_price * buy_hall_count * deduct_period;
            $('#period_money').html(period_money+' 元');
            
            //计算计费总计
            $('#total_money').html('');
            var period_count = $.trim($('#period_count').val());
            check_period_count = eg.test(period_count);
            if (check_period_count) {
                deduct_period_count = period_count/deduct_period;
                var deduct_period_count = Math.ceil(period_count/deduct_period);
                var total_money = period_money * deduct_period_count;
                
                total_money = total_money.toFixed(2);
                
                $('#total_money').html(total_money+' 元');
            }
        }
    }
}

//计算每期扣费总计：下一次
function calculatePeriodMoneyNext()
{
    var hall_price_next = $.trim($('#hall_price_next').val());
    var buy_hall_count_next = $.trim($('#buy_hall_count_next').val());
    var deduct_period_next = $.trim($('#deduct_period_next').val());
    $('#period_money_next').html('');
    if (0 < hall_price_next
        && 0 < buy_hall_count_next
        && 0 < deduct_period_next) {
        var eg = /^[1-9]*[1-9][0-9]*$/;
        check_hall_price = eg.test(hall_price_next);
        check_buy_hall_count = eg.test(buy_hall_count_next);
        check_deduct_period = eg.test(deduct_period_next);
        if (check_hall_price
            && check_buy_hall_count
            && check_deduct_period) {
            var period_money_next = hall_price_next * buy_hall_count_next * deduct_period_next;
            $('#period_money_next').html(period_money_next+' 元');
            
            //计算计费总计
            $('#total_money_next').html('');
            var period_count_next = $.trim($('#period_count_next').val());
            check_period_count = eg.test(period_count_next);
            if (check_period_count) {
                deduct_period_count = period_count_next/deduct_period_next;
                var deduct_period_count = Math.ceil(period_count_next/deduct_period_next);
                var total_money_next = period_money_next * deduct_period_count;
                $('#total_money_next').html(total_money_next+' 元');
            }
        }
    }
}

//选择2016计费方式
function selectBillingVideoPlayType()
{
    var billing_video_play_type = $('#billing_video_play_type').val();
    var recharge_method_id = $('#recharge_method_id').val();
    
    if (0 < billing_video_play_type
        || 0 < agent_billing_video_play_type) {
        $('.not_try_data_box').hide();
        hideMessage('buy_hall_count');
        hideMessage('hall_price');
        hideMessage('period_count');
        hideMessage('deduct_period');
    } else {
        $('.not_try_data_box').show();
    }
}

//更新影院用户
function updateCinemaUser(url, message)
{
    swal({
        title: message,
        text: "",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#0C7AB9",
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        closeOnConfirm: false
        },
        function(){
            $.ajax({
                type:'get',
                dataType:'json',
                url: url,
                data:{
                    option:'ajax'
                },
                success:function(data){
                    if (data.status==1) {
                        alertMessage(data.info + ' 页面即将自动跳转~', 1, 2);
                        if (data.url) {
                            delayGoToPage(data.url, 2000);
                        }
                    }else{
                        alertMessage(data.info, '', 2);
                    }
                }
            });
        }
    );
}

//充值
function recharge()
{
    var recharge_from = $('#recharge_from').val();
    
    var money = $.trim($('#money').val());
    var check_money = false;
    if (!money) {
        showMessage('请输入充值金额', 'money');
    } else {
        //var eg = /^[1-9]*[1-9][0-9]*$/;
        var eg = /^\d+(\.\d{1,2})?$/;
        var check_data = eg.test(money);
        if (!check_data) {
            showMessage('请输入正确的充值金额', 'money');
        } else {
            check_money = true;
            hideMessage('money');   
        }
    }
    
    var description = $.trim($('#description').val());
    var check_description = false;
    if (!description) {
        showMessage('请输入充值凭证', 'description');
    } else {
        check_description = true;
        hideMessage('description');
    }
    
    var img_url = $.trim($('#u0_input').val());
    var check_img_url = false;
    if (!img_url) {
        showMessage('请选择充值凭证图片', 'img_url');
    } else {
        check_img_url = true;
        hideMessage('img_url');
    }
    
    var account_name = $.trim($('#account_name').val());
    var check_account_name = false;
    if (!account_name) {
        showMessage('请输入到账账户', 'account_name');
    } else {
        check_account_name = true;
        hideMessage('account_name');
    }
    
    var account_number = $.trim($('#account_number').val());
    var check_account_number = false;
    if (!account_number) {
        showMessage('请输入正确的账户号码', 'account_number');
    } else {
        var eg = /^(0|[1-9][0-9]*)$/;
        var check_data = eg.test(account_number);
        if (check_data) {
            check_account_number = true;
            hideMessage('account_number');
        } else {
            showMessage('请输入正确的账户号码', 'account_number');
        }
    }
    
    //现金支付，部分字段非必填
    if (2 == recharge_from) {
        check_description = true;
        hideMessage('description');
    }
    
    if(check_money 
       && check_description 
       && check_img_url 
       && check_account_name 
       && check_account_number)
    {
        ajaxPost();
    }
}

//重置表单
function reset(form_id)
{
    //$('#'+form_id)[0].reset();
    history.back(-1);
}

//导航高亮
function highlight_subnav(url){
    $('.side-sub-menu').find(".left_menu_"+musc).closest('li').addClass('active');
}

//选择计费方式类型
function changeCostType()
{
    var cost_type = $('input:radio[name=cost_type]:checked').val();
    if (1 == cost_type) {
        $('#time_count').val('');
        $('#time_type').val(1);
        $('#time_count').attr('disabled', true);
        $('#time_type').attr('disabled', true);
    } else {
        $('#time_count').removeAttr('disabled');
        $('#time_type').removeAttr('disabled');
    }
}

/**
 * 商品分类数据验证
 * @author Geng.Lin
 */
function categoryFormCheck() {
	//分类名称是否为空
	var category_name = $.trim($('#name').val());
    var check_category_name = false;
    if (!category_name) {
        showMessage('请输入分类名称', 'name');
    } else {
    	check_category_name = true;
        hideMessage('name');
    }
    
    //分类描述
//    var description = $.trim($('#description').val());
//    var check_description = false;
//    if (!description) {
//    	showMessage('请输入分类名称', 'description');
//    } else {
//    	check_description = true;
//    	hideMessage('description');
//    }
	
    if(check_category_name 
//    	       && check_description 
    	       ) {
    	        ajaxPost();
    	    }
    
}

/**
 * 选择对话框
 */
function swalMsg(url, message)
{
    swal({
        title: message,
        text: "",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#0C7AB9",
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        closeOnConfirm: false
        },
        function(){
            $.ajax({
                type:'get',
                dataType:'json',
                url: url,
                data:{
                    option:'ajax'
                },
                success:function(data){
                    if (data.status==1) {
                        alertMessage(data.info + ' 页面即将自动跳转~', 1, 2);
                    }else{
                        alertMessage(data.info, '', 2);
                    }
                    if (data.url) {
                        delayGoToPage(data.url, 2000);
                    }else {
                    	delayGoToPage(window.location.href, 2000);
                    }
                }
            });
        }
    );
}

//列表排序
function sort() {
    ajaxPost();
}

//修改计费方式
function changeRecharge()
{
    var hall_price_next = $.trim($('#hall_price_next').val());
    var check_hall_price_next = false;
    if (!hall_price_next) {
        showMessage('请填写计费金额', 'hall_price_next');
    } else if(hall_price_next) {
        //var eg = /^[1-9]*[1-9][0-9]*$/;
        var eg = /^\d+(\.\d{1,2})?$/;
        var check_data = eg.test(hall_price_next);
        if (!check_data) {
            showMessage('请填写计费金额', 'hall_price_next');
        } else {
            check_hall_price_next = true;
            hideMessage('hall_price_next');
        }
    }
    
    var period_count_next = $.trim($('#period_count_next').val());
    var check_period_count_next = false;
    if (!period_count_next) {
        showMessage('请输入合同周期的月数，最大为数字99', 'period_count_next');
    } else if(period_count_next) {
        var eg = /^[1-9]*[1-9][0-9]*$/;
        var check_data = eg.test(period_count_next);
        if (!check_data) {
            showMessage('请输入合同周期的月数，最大为数字99', 'period_count_next');
        } else {
            check_period_count_next = true;
            hideMessage('period_count_next');
        }
    }
    
    var buy_hall_count_next = $.trim($('#buy_hall_count_next').val());
    var check_buy_hall_count_next = false;
    if (!buy_hall_count_next) {
        showMessage('请输入授权房间数量，最大为数字99', 'buy_hall_count_next');
    } else if(buy_hall_count_next) {
        var eg = /^[1-9]*[1-9][0-9]*$/;
        var check_data = eg.test(buy_hall_count_next);
        if (!check_data) {
            showMessage('请输入授权房间数量，最大为数字99', 'buy_hall_count_next');
        } else {
            check_buy_hall_count_next = true;
            hideMessage('buy_hall_count_next');
        }
    }
    
    var deduct_period_next = $.trim($('#deduct_period_next').val());
    var check_deduct_period_next = false;
    if (!deduct_period_next) {
        showMessage('请选择扣费周期', 'deduct_period_next');
    } else {
        check_deduct_period_next = true;
        hideMessage('deduct_period_next');
    }
    
    if(check_hall_price_next
       && check_period_count_next
       && check_buy_hall_count_next
       && check_deduct_period_next)
    {
        ajaxPost();
    }
}

//选择充值来源
function changeRechargeFrom() {
    var recharge_from = $('#recharge_from').val();
    if (2 == recharge_from) {
        $('.not_cash_field').hide();
    } else {
        $('.not_cash_field').show();
    }
}

//选择收款账号
function changeAccountNumber() {
    var account_name = $('#account_name').val();
    $('#account_number').val(account_name);
    $('#account_number_show').html( $('#account_number option:selected').text());
}

//权限菜单选择
function checkSubMenu(obj)
{
    var menu_id = $(obj).val();
    if (true == $(obj).prop('checked')) {
        $('.parent_id_'+menu_id).prop('checked', true);
    } else {
        $('.parent_id_'+menu_id).prop('checked', false);
    }
}

//选择主菜单
function checkMainMenu(obj, menu_id)
{
    if (true == $(obj).prop('checked')) {
        $('.level_1_id_'+menu_id).prop('checked', true);
    } else {
        $('.level_1_id_'+menu_id).prop('checked', false);
    }
}

//展示个人信息编辑框
function showBaseinfoForm()
{
    $('#baseinfo_text').hide();
    $('#baseinfo_form').show();
}

//隐藏个人信息编辑框
function hiddenBaseinfoForm()
{
    $('#baseinfo_form').hide();
    $('#baseinfo_text').show();
    $('#nick_name').val($('#nick_name_std').val());
    $('#mobile').val($('#mobile_std').val());
    hideMessage('nick_name');
    hideMessage('mobile');
}

//修改基本信息
function saveBaseinfo()
{
    var nick_name = $.trim($('#nick_name').val());
    var check_nick_name = false;
    if (!nick_name) {
        showMessage('请输入1-10个字母、数字、中文或下划线', 'nick_name');
        hideMessage('nick_name_info');
    } else if(1 > nick_name.length || 10 < nick_name.length){
        showMessage('请输入1-10个字母、数字、中文或下划线', 'nick_name');
        hideMessage('nick_name_info');
    } else {
        var eg = /^[A-Za-z0-9_\u4E00-\u9FA5]+$/;
        var check_data = eg.test(nick_name);
        if (!check_data) {
            showMessage('请输入1-10个字母、数字、中文或下划线', 'nick_name');
            hideMessage('nick_name_info');
        } else {
            check_nick_name = true;
            hideMessage('nick_name');
        }
    }
    
    var mobile = $.trim($('#mobile').val());
    var check_mobile = false;
    if (!mobile) {
        showMessage('请输入可用的手机号码', 'mobile');
        hideMessage('mobile_info');
    } else if (!checkMobile(mobile)) {
        showMessage('请输入可用的手机号码', 'mobile');
        hideMessage('mobile_info');
    } else {
        check_mobile = true;
        hideMessage('mobile');
    }
    
    if(check_nick_name
       && check_mobile)
    {
        ajaxPost();
    }
}

//修改密码
function savePassword()
{
    var password = $.trim($('#password').val());
    var check_password = false;
    if (!password) {
        showMessage('请输入原密码', 'password');
        hideMessage('password_info');
    } else if(6 > password.length || 16 < password.length){
        showMessage('请输入6-16位字母或数字', 'password');
        hideMessage('password_info');
    } else {
        var eg = /^[A-Za-z0-9]+$/;
        var check_data = eg.test(password);
        if (!check_data) {
            showMessage('请输入6-16位字母或数字', 'password');
            hideMessage('password_info');
        } else {
            check_password = true;
            hideMessage('password');
        }
    }
    
    var new_password = $.trim($('#new_password').val());
    var check_new_password = false;
    if (!new_password) {
        showMessage('请输入6-16位字母或数字', 'new_password');
        hideMessage('new_password_info');
    } else if(6 > new_password.length || 16 < new_password.length){
        showMessage('请输入6-16位字母或数字', 'new_password');
        hideMessage('new_password_info');
    } else {
        var eg = /^[A-Za-z0-9]+$/;
        var check_data = eg.test(new_password);
        if (!check_data) {
            showMessage('请输入6-16位字母或数字', 'new_password');
            hideMessage('new_password_info');
        } else {
            check_new_password = true;
            hideMessage('new_password');
        }
    }
    
    var re_password = $.trim($('#re_password').val());
    var check_re_password = false;
    if (!new_password) {
        showMessage('请再次输入新密码', 're_password');
        hideMessage('re_password_info');
    } else if(6 > re_password.length || 16 < re_password.length){
        showMessage('请输入6-16位字母或数字', 're_password');
        hideMessage('re_password_info');
    } else {
        var eg = /^[A-Za-z0-9]+$/;
        var check_data = eg.test(re_password);
        if (!check_data) {
            showMessage('请输入6-16位字母或数字', 're_password');
            hideMessage('re_password_info');
        } else {
            check_re_password = true;
            hideMessage('re_password');
        }
    }
    
    if (new_password
        && re_password
        && new_password != re_password) {
        check_re_password = false;
        showMessage('新密码与确认密码不一致，请验证', 're_password');
    }
    if(check_password
       && check_new_password
       && check_re_password)
    {
        ajaxPost();
    }
}

//选择全部
function checkAll(target_class)
{
    if($('.checkbox_all').prop('checked')) {
        $('.'+target_class).prop('checked', true);
    } else {
        $('.'+target_class).removeAttr('checked');
    }
}

//批量操作用户
function updateUsers(option)
{
    $('#option').val(option);
    
    if ('stop' == option) {
        var is_select = false;
        $('.data_ids').each(function(){
            if ($(this).prop('checked')) {
                is_select = true;
            }
        });
        if (!is_select) {
            alertMessage('请选择用户~', '', 2);
            return false;
        }
        swal({
            title: "是否确认禁用全部所选择的用户？被禁用的用户将无法登录。",
            text: "",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#0C7AB9",
            cancelButtonText: "取消",
            confirmButtonText: "确定",
            closeOnConfirm: false
            },
            function(){
                ajaxPost();
            }
        );
    } else {
        var is_select = false;
        $('.data_ids').each(function(){
            if ($(this).prop('checked')) {
                is_select = true;
            }
        });
        if (!is_select) {
            alertMessage('请选择用户~', '', 2);
            return false;
        }
        swal({
            title: "是否确认启用全部所选择的用户？",
            text: "",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#0C7AB9",
            cancelButtonText: "取消",
            confirmButtonText: "确定",
            closeOnConfirm: false
            },
            function(){
                ajaxPost();
            }
        );
    }
}